{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with Feature Layers and Features\n",
    "\n",
    "The feature layer is the primary concept for working with features in a GIS.\n",
    "\n",
    "Users create, import, export, analyze, edit, and visualize features, i.e. “entities in space” as feature layers.\n",
    "\n",
    "Feature layers can be added to and visualized using maps. They act as inputs to and outputs from feature analysis tools.\n",
    "\n",
    "Feature layers are created by publishing feature data to a GIS, and are exposed as a broader resource (Item) in the GIS. Feature layer instances can be obtained through the layers attribute on feature layer collection Items in the GIS. A feature layer collection is a collection of feature layers and tables, with the associated relationships among the entities. A feature layer collection is backed by a [feature service](/rest/services-reference/enterprise/feature-service/) in a web GIS. See [What is a feature service?](https://enterprise.arcgis.com/en/server/latest/publish-services/windows/what-is-a-feature-service-.htm) for more details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Working-with-Feature-Layers-and-Features\" data-toc-modified-id=\"Working-with-Feature-Layers-and-Features-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Working with Feature Layers and Features</a></span><ul class=\"toc-item\"><li><span><a href=\"#Accessing-Feature-Layers\" data-toc-modified-id=\"Accessing-Feature-Layers-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Accessing Feature Layers</a></span></li><li><span><a href=\"#Searching-the-GIS-for-feature-layers\" data-toc-modified-id=\"Searching-the-GIS-for-feature-layers-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Searching the GIS for feature layers</a></span></li><li><span><a href=\"#Accessing-feature-layers-using-item-id\" data-toc-modified-id=\"Accessing-feature-layers-using-item-id-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>Accessing feature layers using item id</a></span></li><li><span><a href=\"#Accessing-feature-layers-and-tables-from-feature-services\" data-toc-modified-id=\"Accessing-feature-layers-and-tables-from-feature-services-1.4\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>Accessing feature layers and tables from feature services</a></span></li><li><span><a href=\"#Accessing-feature-layers-from-a-feature-layer-url\" data-toc-modified-id=\"Accessing-feature-layers-from-a-feature-layer-url-1.5\"><span class=\"toc-item-num\">1.5&nbsp;&nbsp;</span>Accessing feature layers from a feature layer url</a></span></li><li><span><a href=\"#Properties-of-FeatureLayer\" data-toc-modified-id=\"Properties-of-FeatureLayer-1.6\"><span class=\"toc-item-num\">1.6&nbsp;&nbsp;</span>Properties of FeatureLayer</a></span></li><li><span><a href=\"#Querying-feature-layers\" data-toc-modified-id=\"Querying-feature-layers-1.7\"><span class=\"toc-item-num\">1.7&nbsp;&nbsp;</span>Querying feature layers</a></span></li><li><span><a href=\"#Querying-features-using-a-different-spatial-reference\" data-toc-modified-id=\"Querying-features-using-a-different-spatial-reference-1.8\"><span class=\"toc-item-num\">1.8&nbsp;&nbsp;</span>Querying features using a different spatial reference</a></span></li><li><span><a href=\"#FeatureSet-properties\" data-toc-modified-id=\"FeatureSet-properties-1.9\"><span class=\"toc-item-num\">1.9&nbsp;&nbsp;</span>FeatureSet properties</a></span></li><li><span><a href=\"#Accessing-Features-from-query-results\" data-toc-modified-id=\"Accessing-Features-from-query-results-1.10\"><span class=\"toc-item-num\">1.10&nbsp;&nbsp;</span>Accessing Features from query results</a></span></li><li><span><a href=\"#Accessing-Feature-geometry-and-attributes\" data-toc-modified-id=\"Accessing-Feature-geometry-and-attributes-1.11\"><span class=\"toc-item-num\">1.11&nbsp;&nbsp;</span>Accessing Feature geometry and attributes</a></span></li></ul></li><li><span><a href=\"#Using-feature-collections\" data-toc-modified-id=\"Using-feature-collections-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Using feature collections</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing Feature Layers\n",
    "\n",
    "Feature layer collection items are available as content in the GIS. You can search the GIS for feature layer collection items, or get them using their item id. \n",
    "\n",
    "Feature layers are available through the layers attribute on feature layer collection Items in the GIS."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Searching the GIS for feature layers \n",
    "\n",
    "You can search the GIS for feature layer collections by specifying the item type as 'Feature Layer Collection' or 'Feature Layer'.\n",
    "\n",
    "Note: A feature layer collection can be considered a type of feature layer such as a group feature layer. Hence, you can specify the item type as 'Feature Layer' and still get back feature layer collection items as results.\n",
    "\n",
    "The examples below will clarify this further:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Establish a connection to your GIS.\n",
    "from arcgis.gis import GIS\n",
    "from IPython.display import display\n",
    "gis = GIS() # anonymous connection to www.arcgis.com"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=85d0ca4ea1ca4b9abf0c51b9bd34de2e' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/85d0ca4ea1ca4b9abf0c51b9bd34de2e/info/thumbnail/thumbnail1578438561290.jpeg' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=85d0ca4ea1ca4b9abf0c51b9bd34de2e' target='_blank'><b>USA Major Cities</b>\n",
       "                        </a>\n",
       "                        <br/>This layer presents the locations of cities within the United States with populations of approximately 10,000 or greater, all state capitals, and the national capital.<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by esri_dm\n",
       "                        <br/>Last Modified: May 19, 2020\n",
       "                        <br/>3 comments, 35821202 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"USA Major Cities\" type:Feature Layer Collection owner:esri_dm>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Search for 'USA major cities' feature layer collection\n",
    "search_results = gis.content.search('title: USA Major Cities',\n",
    "                                    'Feature Layer')\n",
    "\n",
    "# Access the first Item that's returned\n",
    "major_cities_item = search_results[0]\n",
    "\n",
    "major_cities_item"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the major_cities_item is a 'Feature Layer Collection' item.\n",
    "Since this item is a Feature Layer Collection, accessing the `layers` property will give us a list of `FeatureLayer` objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<FeatureLayer url:\"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Major_Cities/FeatureServer/0\">]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "major_cities_layers = major_cities_item.layers\n",
    "major_cities_layers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing feature layers using item id\n",
    "\n",
    "Feature layer collection items are available as content in the GIS. You can get them using their item id, and query their layers property to get to the feature layers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=91c6a5f6410b4991ab0db1d7c26daacb' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/91c6a5f6410b4991ab0db1d7c26daacb/info/thumbnail/thumbnail1578437765660.jpeg' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=91c6a5f6410b4991ab0db1d7c26daacb' target='_blank'><b>USA Freeway System</b>\n",
       "                        </a>\n",
       "                        <br/>This layer presents rural and urban interstate highways.<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by esri_dm\n",
       "                        <br/>Last Modified: January 07, 2020\n",
       "                        <br/>2 comments, 4321723 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"USA Freeway System\" type:Feature Layer Collection owner:esri_dm>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "freeways = gis.content.get('91c6a5f6410b4991ab0db1d7c26daacb')\n",
    "freeways"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since freeways is a Feature Layer Collection item, accessing the layers property will give us a list of FeatureLayer objects. This item has two layers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<FeatureLayer url:\"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Freeway_System/FeatureServer/1\">,\n",
       " <FeatureLayer url:\"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Freeway_System/FeatureServer/2\">]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "freeways.layers "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The code below cycles through the layers and prints their names:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "USA Freeway System (over 1:500k)\n",
      "USA Freeway System (below 1:500k)\n"
     ]
    }
   ],
   "source": [
    "for lyr in freeways.layers:\n",
    "    print(lyr.properties.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing feature layers and tables from feature services\n",
    "\n",
    "A feature service serves a collection of feature layers and tables, with the associated relationships among the entities. It is represented by `arcgis.features.FeatureLayerCollection` in the ArcGIS Python API.\n",
    "\n",
    "Instances of FeatureLayerCollection can be constructed using a feature service url, as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.features import FeatureLayerCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "fs_url = 'https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer'\n",
    "sanfran = FeatureLayerCollection(fs_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The collection of layers and tables in a FeatureLayerCollection can be accessed using the layers and tables properties respectively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<FeatureLayer url:\"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0\">]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sanfran.layers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Tables** represent entity classes with uniform properties. In addition to working with “entities with location” as features, the GIS can also work with non-spatial entities as rows in tables. Working with tables is similar to working with feature layers, except that the rows (Features) in a table do not have a geometry, and tables ignore any geometry related operation. \n",
    "\n",
    "The sanfran feature layer collection also has a table that can be obtained using its tables property:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Table url:\"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/1\">]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sanfran.tables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing feature layers from a feature layer url\n",
    "\n",
    "Instances of FeatureLayers can also be constructed using a url to the REST endpoint of a feature layer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.features import FeatureLayer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<FeatureLayer url:\"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0\">"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lyr_url = 'https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0'\n",
    "\n",
    "layer = FeatureLayer(lyr_url)\n",
    "layer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"featurelayer-properties\"></a>\n",
    "## Properties of FeatureLayer\n",
    "In this section, let us take a closer look at the properties of a `FeatureLayer` object. We will use the `major_cities_layers` object created earlier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<FeatureLayer url:\"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Major_Cities/FeatureServer/0\">"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer = major_cities_item.layers[0]\n",
    "feature_layer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `properties` field on a `FeatureLayer` object provides a dictionary representation of all its properties. However you can access individual properties as fields as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"xmin\": -17608123.3895845,\n",
       "  \"ymin\": 2237818.89912024,\n",
       "  \"xmax\": -7656889.54512499,\n",
       "  \"ymax\": 9568526.08684907,\n",
       "  \"spatialReference\": {\n",
       "    \"wkid\": 102100,\n",
       "    \"latestWkid\": 3857\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer.properties.extent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `capabilities` property is useful to know what kinds of edits and operations be performed on the feature layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Query'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer.properties.capabilities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can access the rendering information from the `drawingInfo` property"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'classBreaks'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer.properties.drawingInfo.renderer.type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"querying-layers\"></a>\n",
    "## Querying feature layers\n",
    "Querying is a powerful operation that can be performed on a [`FeatureLayer`](/python/latest/api-reference/arcgis.features.toc.html#arcgis.features.FeatureLayer) object. Let's take a closer look here. To write meaningful queries, we need to know the names of fields present in the layer. This can be determined by calling the `fields` property:\n",
    "\n",
    "> **Note:** When leveraging Esri hosted content, organizations should review the [ArcGIS Online terms of use](https://doc.arcgis.com/en/arcgis-online/reference/terms-of-use.htm), as well as the terms of use for the data layer to ensure they are in compliance with extracting data and/or making it available in other systems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FID\n",
      "NAME\n",
      "CLASS\n",
      "ST\n",
      "STFIPS\n",
      "PLACEFIPS\n",
      "CAPITAL\n",
      "POP_CLASS\n",
      "POPULATION\n",
      "POP2010\n",
      "WHITE\n",
      "BLACK\n",
      "AMERI_ES\n",
      "ASIAN\n",
      "HAWN_PI\n",
      "HISPANIC\n",
      "OTHER\n",
      "MULT_RACE\n",
      "MALES\n",
      "FEMALES\n",
      "AGE_UNDER5\n",
      "AGE_5_9\n",
      "AGE_10_14\n",
      "AGE_15_19\n",
      "AGE_20_24\n",
      "AGE_25_34\n",
      "AGE_35_44\n",
      "AGE_45_54\n",
      "AGE_55_64\n",
      "AGE_65_74\n",
      "AGE_75_84\n",
      "AGE_85_UP\n",
      "MED_AGE\n",
      "MED_AGE_M\n",
      "MED_AGE_F\n",
      "HOUSEHOLDS\n",
      "AVE_HH_SZ\n",
      "HSEHLD_1_M\n",
      "HSEHLD_1_F\n",
      "MARHH_CHD\n",
      "MARHH_NO_C\n",
      "MHH_CHILD\n",
      "FHH_CHILD\n",
      "FAMILIES\n",
      "AVE_FAM_SZ\n",
      "HSE_UNITS\n",
      "VACANT\n",
      "OWNER_OCC\n",
      "RENTER_OCC\n"
     ]
    }
   ],
   "source": [
    "for f in feature_layer.properties.fields:\n",
    "    print(f['name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The query method has a number of parameters that allow you to refine and transform the results. Since the processing is performed on the server, this operation is not restricted by the capacity of the client computer. \n",
    "\n",
    "For instance, let us select all the cities whose population in the year 2010 was greater than 1 million. Instead of returning all the fields, let us get only population related fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result1 = feature_layer.query(where='POP2010>1000000', \n",
    "                                    out_fields='WHITE,BLACK,MULT_RACE,HISPANIC')\n",
    "len(query_result1.features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'FID',\n",
       "  'type': 'esriFieldTypeOID',\n",
       "  'alias': 'FID',\n",
       "  'sqlType': 'sqlTypeInteger',\n",
       "  'domain': None,\n",
       "  'defaultValue': None},\n",
       " {'name': 'WHITE',\n",
       "  'type': 'esriFieldTypeInteger',\n",
       "  'alias': 'WHITE',\n",
       "  'sqlType': 'sqlTypeInteger',\n",
       "  'domain': None,\n",
       "  'defaultValue': None},\n",
       " {'name': 'BLACK',\n",
       "  'type': 'esriFieldTypeInteger',\n",
       "  'alias': 'BLACK',\n",
       "  'sqlType': 'sqlTypeInteger',\n",
       "  'domain': None,\n",
       "  'defaultValue': None},\n",
       " {'name': 'MULT_RACE',\n",
       "  'type': 'esriFieldTypeInteger',\n",
       "  'alias': 'MULT_RACE',\n",
       "  'sqlType': 'sqlTypeInteger',\n",
       "  'domain': None,\n",
       "  'defaultValue': None},\n",
       " {'name': 'HISPANIC',\n",
       "  'type': 'esriFieldTypeInteger',\n",
       "  'alias': 'HISPANIC',\n",
       "  'sqlType': 'sqlTypeInteger',\n",
       "  'domain': None,\n",
       "  'defaultValue': None}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result1.fields"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we are only interested in the count, we could save bandwidth by setting the `return_count_only` to `True`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer.query(where='POP2010>1000000', return_count_only=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying features using a different spatial reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'wkid': 102100, 'latestWkid': 3857}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result1.spatial_reference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, the query results are in the same spatial reference as the source layer. However you can use the `out_sr` parameter to reproject the result into a desired spatial reference. The projection happens on the server and on all the resulting features.\n",
    "\n",
    "In the example above, we obtained data in wkid:3857, a well known id for 'Web Mercator' projection. We can observe how the coordinates look like below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': -9756835.705284344,\n",
       " 'y': 5124572.044517264,\n",
       " 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result1.features[0].geometry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The coordinates are in projected coordinate system as expected. If we wish to have this data in latitude and longitude instead, we could do so by changing the `out_sr` to wkid:4326"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id = \"featureset-properties\"></a>\n",
    "## FeatureSet properties\n",
    "As seen previously, a `FeatureSet` is returned by a `query()` operation. The `FeatureSet` object packs a bunch of useful properties that help us discern useful information about the features under access"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One of the important properties is the `spatial_reference` as you saw earlier. Below, we are using the same `query_result1` `FeatureSet` from earlier query operation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'wkid': 102100, 'latestWkid': 3857}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result1.spatial_reference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One of the most powerful operation on a `FeatureSet` is accessing the features not as `Feature` objects, but as pandas dataframe objects. The `sdf` property, returns a dataframe object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>FID</th>\n",
       "      <th>NAME</th>\n",
       "      <th>CLASS</th>\n",
       "      <th>ST</th>\n",
       "      <th>STFIPS</th>\n",
       "      <th>PLACEFIPS</th>\n",
       "      <th>CAPITAL</th>\n",
       "      <th>POP_CLASS</th>\n",
       "      <th>POPULATION</th>\n",
       "      <th>POP2010</th>\n",
       "      <th>...</th>\n",
       "      <th>MARHH_NO_C</th>\n",
       "      <th>MHH_CHILD</th>\n",
       "      <th>FHH_CHILD</th>\n",
       "      <th>FAMILIES</th>\n",
       "      <th>AVE_FAM_SZ</th>\n",
       "      <th>HSE_UNITS</th>\n",
       "      <th>VACANT</th>\n",
       "      <th>OWNER_OCC</th>\n",
       "      <th>RENTER_OCC</th>\n",
       "      <th>SHAPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>63</td>\n",
       "      <td>Chicago</td>\n",
       "      <td>city</td>\n",
       "      <td>IL</td>\n",
       "      <td>17</td>\n",
       "      <td>1714000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>2781116</td>\n",
       "      <td>2695598</td>\n",
       "      <td>...</td>\n",
       "      <td>168435</td>\n",
       "      <td>28332</td>\n",
       "      <td>116555</td>\n",
       "      <td>576793</td>\n",
       "      <td>3.4</td>\n",
       "      <td>1194337</td>\n",
       "      <td>148777</td>\n",
       "      <td>469562</td>\n",
       "      <td>575998</td>\n",
       "      <td>{\"x\": -9756835.705284344, \"y\": 5124572.0445172...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>415</td>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>city</td>\n",
       "      <td>CA</td>\n",
       "      <td>06</td>\n",
       "      <td>0644000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>3986442</td>\n",
       "      <td>3792621</td>\n",
       "      <td>...</td>\n",
       "      <td>245056</td>\n",
       "      <td>46908</td>\n",
       "      <td>118939</td>\n",
       "      <td>807326</td>\n",
       "      <td>3.53</td>\n",
       "      <td>1413995</td>\n",
       "      <td>95827</td>\n",
       "      <td>503863</td>\n",
       "      <td>814305</td>\n",
       "      <td>{\"x\": -13165820.99345245, \"y\": 4035892.5853389...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>550</td>\n",
       "      <td>San Diego</td>\n",
       "      <td>city</td>\n",
       "      <td>CA</td>\n",
       "      <td>06</td>\n",
       "      <td>0666000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>1397856</td>\n",
       "      <td>1307402</td>\n",
       "      <td>...</td>\n",
       "      <td>105134</td>\n",
       "      <td>12062</td>\n",
       "      <td>33865</td>\n",
       "      <td>285221</td>\n",
       "      <td>3.28</td>\n",
       "      <td>516033</td>\n",
       "      <td>32941</td>\n",
       "      <td>233158</td>\n",
       "      <td>249934</td>\n",
       "      <td>{\"x\": -13040584.546974456, \"y\": 3858240.841215...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>910</td>\n",
       "      <td>Phoenix</td>\n",
       "      <td>city</td>\n",
       "      <td>AZ</td>\n",
       "      <td>04</td>\n",
       "      <td>0455000</td>\n",
       "      <td>State</td>\n",
       "      <td>10</td>\n",
       "      <td>1601381</td>\n",
       "      <td>1445632</td>\n",
       "      <td>...</td>\n",
       "      <td>102047</td>\n",
       "      <td>23353</td>\n",
       "      <td>54452</td>\n",
       "      <td>330762</td>\n",
       "      <td>3.42</td>\n",
       "      <td>590149</td>\n",
       "      <td>75343</td>\n",
       "      <td>296742</td>\n",
       "      <td>218064</td>\n",
       "      <td>{\"x\": -12476005.927345268, \"y\": 3954668.990210...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2174</td>\n",
       "      <td>New York</td>\n",
       "      <td>city</td>\n",
       "      <td>NY</td>\n",
       "      <td>36</td>\n",
       "      <td>3651000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>8691599</td>\n",
       "      <td>8175133</td>\n",
       "      <td>...</td>\n",
       "      <td>566484</td>\n",
       "      <td>77061</td>\n",
       "      <td>344755</td>\n",
       "      <td>1850221</td>\n",
       "      <td>3.32</td>\n",
       "      <td>3371062</td>\n",
       "      <td>261278</td>\n",
       "      <td>962892</td>\n",
       "      <td>2146892</td>\n",
       "      <td>{\"x\": -8238770.183515309, \"y\": 4969744.1655956...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2987</td>\n",
       "      <td>Philadelphia</td>\n",
       "      <td>city</td>\n",
       "      <td>PA</td>\n",
       "      <td>42</td>\n",
       "      <td>4260000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>1587761</td>\n",
       "      <td>1526006</td>\n",
       "      <td>...</td>\n",
       "      <td>95310</td>\n",
       "      <td>18486</td>\n",
       "      <td>86946</td>\n",
       "      <td>340354</td>\n",
       "      <td>3.2</td>\n",
       "      <td>670171</td>\n",
       "      <td>70435</td>\n",
       "      <td>324536</td>\n",
       "      <td>275200</td>\n",
       "      <td>{\"x\": -8366882.6254769275, \"y\": 4858876.345453...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3592</td>\n",
       "      <td>Dallas</td>\n",
       "      <td>city</td>\n",
       "      <td>TX</td>\n",
       "      <td>48</td>\n",
       "      <td>4819000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>1323651</td>\n",
       "      <td>1197816</td>\n",
       "      <td>...</td>\n",
       "      <td>76902</td>\n",
       "      <td>14512</td>\n",
       "      <td>50242</td>\n",
       "      <td>265538</td>\n",
       "      <td>3.42</td>\n",
       "      <td>516639</td>\n",
       "      <td>58582</td>\n",
       "      <td>201880</td>\n",
       "      <td>256177</td>\n",
       "      <td>{\"x\": -10775254.59703981, \"y\": 3865959.6760583...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3641</td>\n",
       "      <td>Houston</td>\n",
       "      <td>city</td>\n",
       "      <td>TX</td>\n",
       "      <td>48</td>\n",
       "      <td>4835000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>2333285</td>\n",
       "      <td>2099451</td>\n",
       "      <td>...</td>\n",
       "      <td>145478</td>\n",
       "      <td>26039</td>\n",
       "      <td>85014</td>\n",
       "      <td>481570</td>\n",
       "      <td>3.38</td>\n",
       "      <td>892646</td>\n",
       "      <td>110003</td>\n",
       "      <td>355236</td>\n",
       "      <td>427407</td>\n",
       "      <td>{\"x\": -10616262.315905476, \"y\": 3472578.411494...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3726</td>\n",
       "      <td>San Antonio</td>\n",
       "      <td>city</td>\n",
       "      <td>TX</td>\n",
       "      <td>48</td>\n",
       "      <td>4865000</td>\n",
       "      <td></td>\n",
       "      <td>10</td>\n",
       "      <td>1442472</td>\n",
       "      <td>1327407</td>\n",
       "      <td>...</td>\n",
       "      <td>100314</td>\n",
       "      <td>16067</td>\n",
       "      <td>56901</td>\n",
       "      <td>318043</td>\n",
       "      <td>3.34</td>\n",
       "      <td>524246</td>\n",
       "      <td>44604</td>\n",
       "      <td>271070</td>\n",
       "      <td>208572</td>\n",
       "      <td>{\"x\": -10964134.924024679, \"y\": 3429663.909911...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    FID          NAME CLASS  ST STFIPS PLACEFIPS CAPITAL  POP_CLASS  \\\n",
       "0    63       Chicago  city  IL     17   1714000                 10   \n",
       "1   415   Los Angeles  city  CA     06   0644000                 10   \n",
       "2   550     San Diego  city  CA     06   0666000                 10   \n",
       "3   910       Phoenix  city  AZ     04   0455000   State         10   \n",
       "4  2174      New York  city  NY     36   3651000                 10   \n",
       "5  2987  Philadelphia  city  PA     42   4260000                 10   \n",
       "6  3592        Dallas  city  TX     48   4819000                 10   \n",
       "7  3641       Houston  city  TX     48   4835000                 10   \n",
       "8  3726   San Antonio  city  TX     48   4865000                 10   \n",
       "\n",
       "   POPULATION  POP2010  ...  MARHH_NO_C  MHH_CHILD  FHH_CHILD  FAMILIES  \\\n",
       "0     2781116  2695598  ...      168435      28332     116555    576793   \n",
       "1     3986442  3792621  ...      245056      46908     118939    807326   \n",
       "2     1397856  1307402  ...      105134      12062      33865    285221   \n",
       "3     1601381  1445632  ...      102047      23353      54452    330762   \n",
       "4     8691599  8175133  ...      566484      77061     344755   1850221   \n",
       "5     1587761  1526006  ...       95310      18486      86946    340354   \n",
       "6     1323651  1197816  ...       76902      14512      50242    265538   \n",
       "7     2333285  2099451  ...      145478      26039      85014    481570   \n",
       "8     1442472  1327407  ...      100314      16067      56901    318043   \n",
       "\n",
       "   AVE_FAM_SZ  HSE_UNITS  VACANT  OWNER_OCC  RENTER_OCC  \\\n",
       "0         3.4    1194337  148777     469562      575998   \n",
       "1        3.53    1413995   95827     503863      814305   \n",
       "2        3.28     516033   32941     233158      249934   \n",
       "3        3.42     590149   75343     296742      218064   \n",
       "4        3.32    3371062  261278     962892     2146892   \n",
       "5         3.2     670171   70435     324536      275200   \n",
       "6        3.42     516639   58582     201880      256177   \n",
       "7        3.38     892646  110003     355236      427407   \n",
       "8        3.34     524246   44604     271070      208572   \n",
       "\n",
       "                                               SHAPE  \n",
       "0  {\"x\": -9756835.705284344, \"y\": 5124572.0445172...  \n",
       "1  {\"x\": -13165820.99345245, \"y\": 4035892.5853389...  \n",
       "2  {\"x\": -13040584.546974456, \"y\": 3858240.841215...  \n",
       "3  {\"x\": -12476005.927345268, \"y\": 3954668.990210...  \n",
       "4  {\"x\": -8238770.183515309, \"y\": 4969744.1655956...  \n",
       "5  {\"x\": -8366882.6254769275, \"y\": 4858876.345453...  \n",
       "6  {\"x\": -10775254.59703981, \"y\": 3865959.6760583...  \n",
       "7  {\"x\": -10616262.315905476, \"y\": 3472578.411494...  \n",
       "8  {\"x\": -10964134.924024679, \"y\": 3429663.909911...  \n",
       "\n",
       "[9 rows x 50 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query2 = feature_layer.query(where=\"POP2010 > 1000000\")\n",
    "query2.sdf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Accessing the features as a dataframe makes if easier to analyze the data statistically."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing Features from query results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': -87.64714638699998,\n",
       " 'y': 41.75648806100002,\n",
       " 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_geographic = feature_layer.query(where='POP2010 > 1000000', out_sr='4326')\n",
    "query_geographic.features[0].geometry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can execute the `query()` method on the first `FeatureLayer` object and get a `FeatureSet`. Let us query and access the first 10 features in this layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "arcgis.features.feature.FeatureSet"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "major_cities_l1 = major_cities_item.layers[0]\n",
    "major_cities_l1_fset = major_cities_l1.query(where= 'FID < 11')\n",
    "type(major_cities_l1_fset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, accessing the `features` property of the above `FeatureSet` object will provide us the individual point `Features`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "major_cities_l1_features = major_cities_l1_fset.features\n",
    "len(major_cities_l1_features)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"feature-properties\"></a>\n",
    "## Accessing Feature geometry and attributes\n",
    "As mentioned earlier, the `Feature` object is a fine grained representation of spatial information. Two important properties of a `Feature` object are its `geometry` and `attributes`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us display the geometry and attributes of the first feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': -12462673.723706165,\n",
       " 'y': 5384674.994080178,\n",
       " 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "major_cities_l1_features[0].geometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'FID': 1,\n",
       " 'NAME': 'Ammon',\n",
       " 'CLASS': 'city',\n",
       " 'ST': 'ID',\n",
       " 'STFIPS': '16',\n",
       " 'PLACEFIPS': '1601990',\n",
       " 'CAPITAL': ' ',\n",
       " 'POP_CLASS': 6,\n",
       " 'POPULATION': 15181,\n",
       " 'POP2010': 13816,\n",
       " 'WHITE': 13002,\n",
       " 'BLACK': 73,\n",
       " 'AMERI_ES': 67,\n",
       " 'ASIAN': 113,\n",
       " 'HAWN_PI': 9,\n",
       " 'HISPANIC': 884,\n",
       " 'OTHER': 307,\n",
       " 'MULT_RACE': 245,\n",
       " 'MALES': 6750,\n",
       " 'FEMALES': 7066,\n",
       " 'AGE_UNDER5': 1468,\n",
       " 'AGE_5_9': 1503,\n",
       " 'AGE_10_14': 1313,\n",
       " 'AGE_15_19': 1058,\n",
       " 'AGE_20_24': 734,\n",
       " 'AGE_25_34': 2031,\n",
       " 'AGE_35_44': 1767,\n",
       " 'AGE_45_54': 1446,\n",
       " 'AGE_55_64': 1136,\n",
       " 'AGE_65_74': 665,\n",
       " 'AGE_75_84': 486,\n",
       " 'AGE_85_UP': 209,\n",
       " 'MED_AGE': 29.6,\n",
       " 'MED_AGE_M': 28,\n",
       " 'MED_AGE_F': 30.8,\n",
       " 'HOUSEHOLDS': 4476,\n",
       " 'AVE_HH_SZ': 3.05,\n",
       " 'HSEHLD_1_M': 457,\n",
       " 'HSEHLD_1_F': 648,\n",
       " 'MARHH_CHD': 1618,\n",
       " 'MARHH_NO_C': 1131,\n",
       " 'MHH_CHILD': 106,\n",
       " 'FHH_CHILD': 335,\n",
       " 'FAMILIES': 3352,\n",
       " 'AVE_FAM_SZ': 3.61,\n",
       " 'HSE_UNITS': 4747,\n",
       " 'VACANT': 271,\n",
       " 'OWNER_OCC': 3205,\n",
       " 'RENTER_OCC': 1271}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "major_cities_l1_features[0].attributes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"features-from-a-feature-collection\"></a>\n",
    "# Using feature collections\n",
    "\n",
    "Similar to feature layers, [feature collections](https://doc.arcgis.com/en/arcgis-online/reference/feature-layers.htm#GUID-304463C0-25BD-4FEA-8DD6-AD82F2C96B56) can also be used to store features. With a feature collection, a service is not created to serve out feature data. Instead it is stored as json data with the item. Feature collections can be added to maps as layers, passed as input to feature analysis tools and queried for feature data. See the [featureCollection](/web-map-specification/objects/featureCollection/) for additional details.\n",
    "\n",
    "Feature collections are shared in the GIS as items. Feature Collection Items can be searched by specifying 'Feature Collection' as the item_type.\n",
    "\n",
    "Let us search for feature collection items published by Esri Media as an example:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Searching for Feature Collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=e8a87ec5afbf4f908e758855ecc1bcef' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/e8a87ec5afbf4f908e758855ecc1bcef/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=e8a87ec5afbf4f908e758855ecc1bcef' target='_blank'><b>Crime Potential (Esri)</b>\n",
       "                        </a>\n",
       "                        <br/>2014 Crime Potential (Esri)<img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/features16.png' style=\"vertical-align:middle;\">Feature Collection by EsriMedia\n",
       "                        <br/>Last Modified: July 24, 2015\n",
       "                        <br/>0 comments, 233 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Crime Potential (Esri)\" type:Feature Collection owner:EsriMedia>"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_fc = gis.content.search(\"owner:EsriMedia\", item_type='Feature Collection', max_items=20)\n",
    "em_fc_item = search_fc[1]\n",
    "em_fc_item"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Accessing the `layers` property on a feature collection item returns a list of `FeatureCollection` objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<FeatureCollection>]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "em_fc_item.layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "crime_pot_fc = em_fc_item.layers[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying the Feature Collection\n",
    "You can call the `query()` method on a `FeatureCollection` object to get a `FeatureSet`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "crime_fset = crime_pot_fc.query()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing features from a Feature Collection\n",
    "Once you have a `FeatureSet` object, you can access the `features` property to get a list of `Feature` objects as seen earlier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'rings': [[[-13162769, 4027325],\n",
       "   [-13162572, 4027320],\n",
       "   [-13162548, 4027353],\n",
       "   [-13162307, 4027204],\n",
       "   [-13162293, 4027223],\n",
       "   [-13162309, 4027351],\n",
       "   [-13162179, 4027349],\n",
       "   [-13162165, 4027265],\n",
       "   [-13162142, 4027265],\n",
       "   [-13162097, 4027023],\n",
       "   [-13162120, 4027016],\n",
       "   [-13162117, 4026993],\n",
       "   [-13162001, 4026344],\n",
       "   [-13161974, 4026340],\n",
       "   [-13161800, 4025409],\n",
       "   [-13161828, 4025409],\n",
       "   [-13161809, 4025235],\n",
       "   [-13161662, 4024432],\n",
       "   [-13161632, 4024432],\n",
       "   [-13161536, 4024043],\n",
       "   [-13161424, 4023822],\n",
       "   [-13161405, 4023733],\n",
       "   [-13161505, 4023717],\n",
       "   [-13161459, 4023479],\n",
       "   [-13161486, 4023475],\n",
       "   [-13161482, 4023448],\n",
       "   [-13161625, 4023452],\n",
       "   [-13161625, 4023340],\n",
       "   [-13161696, 4023422],\n",
       "   [-13161785, 4023452],\n",
       "   [-13162794, 4023452],\n",
       "   [-13162836, 4023413],\n",
       "   [-13162883, 4023452],\n",
       "   [-13165214, 4023450],\n",
       "   [-13165223, 4027359],\n",
       "   [-13163604, 4027386],\n",
       "   [-13163280, 4027371],\n",
       "   [-13163256, 4027336],\n",
       "   [-13162769, 4027325]]],\n",
       " 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crime_features = crime_fset.features\n",
    "crime_features[0].geometry"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
